#from django.conf import settings

#import locale
import math
import random

from EE.models import Game
from EE.models import GameState
from EE.models import Island
from EE.models import Decision
from EE.models import StateMill

from EE.logic import game as game_logic
from EE.logic import game_state as game_state_logic
from EE.logic import state_mill as state_mill_logic
from EE.logic import island as island_logic

from django.contrib.auth.models import User

# Generating a windforce 
def generate_windforce():    
	random.seed(None)
	windforce = int(random.normalvariate(4,2))
	if windforce <= 0:
		windforce = 0
	elif windforce >= 8:
		windforce = 8
	return windforce

# Generating chance of storm: Will it be stormy this month
def chance_of_storm():
	storm = False
    
	random.seed(None)
	chance = random.normalvariate(1,0.5)
	if (chance > 1.85):
		storm = True
	return storm

# How many windmills are breaking down       
def breakdown_windmill(ref_state):

    windmills_broken = 0

    state_mills_for_game_state = state_mill_logic.getStateMills(ref_state)
    
    for mill in state_mills_for_game_state:
        no_windmills = mill.noOfMills         
    
        maintenance = mill.noOfMaintenance
        no_maintenance = no_windmills - maintenance
        
        for i in range(1, maintenance + 1):
            random.seed(None)
            chance = random.randint(0,100)
            if (chance > 95):
                windmills_broken = windmills_broken + 1
                #print 'Er is een windmolen met onderhoudscontract stuk'
    
        for u in range(1, no_maintenance +1):
            random.seed(None)
            chance = random.randint(0,100)
            if (chance > 90):
                windmills_broken = windmills_broken + 1
                #print 'Er is een windmolen zonder onderhoudscontract stuk'
            
    return windmills_broken

def month_output(ref_user):    
    #Should all come from Database
	game = game_logic.getGame(ref_user)
	ref_state = game_state_logic.getState(ref_user)
	island = island_logic.getIsland(ref_state)
	
	current_buffer = island.currentContent
	max_buffer = island.capacity
	capacity_lost = 0
	not_delivered = 0
	days_in_month = 28
	day_usage = 10000 # afhankelijk van contracten...

	storm = game.storm

	for i in range(1, days_in_month+1):
		day_surplus = 0
		day_output = 0
		windforce = generate_windforce()
		if (storm == True):
			windforce = windforce + 3
                    
		state_mills_for_game_state = state_mill_logic.getStateMills(ref_state)
    
		for mill in state_mills_for_game_state:
			no_windmills = mill.noOfMills
			windmill = mill.typeOfMill
			capacity = windmill.capacity
			
			if (windforce == 3):
				day_output = day_output + no_windmills * capacity * 0.2
			elif (windforce == 4):
				day_output = day_output + no_windmills * capacity * 0.4
			elif (windforce == 5):
				day_output = day_output + no_windmills * capacity * 0.6
			elif (windforce == 6):
				day_output = day_output + no_windmills * capacity * 0.8
			elif (windforce == 7):
				day_output = day_output + no_windmills * capacity * 0.9
			elif (windforce == 8):
				day_output = day_output + no_windmills * capacity
			else:
				day_output = 0
				break
            
		day_surplus = (day_output * 24) - day_usage     
         
		if day_surplus > 0:
			current_buffer = current_buffer + day_surplus
			if current_buffer > max_buffer:
				capacity_lost = capacity_lost + (current_buffer - max_buffer)
				current_buffer = max_buffer
		elif day_surplus < 0:
			if (0.5 * current_buffer) > math.fabs(day_surplus):
				current_buffer = current_buffer - (2 * math.fabs(day_surplus))
			elif (0.5 * current_buffer) < math.fabs(day_surplus):
				not_delivered = not_delivered + (math.fabs(day_surplus) - (0.5 * current_buffer))
				current_buffer = 0
        
#        #print '===== Nieuwe dag ====== '
#        #print i
#        #print 'Is de windkracht'
#        #print windforce
#        if (storm == True):
#            #print 'Het stormt vandaag'
#        #print 'Is de output'
#        #print day_output
#        #print 'Het overschot is ' 
#        #print day_surplus    
#        #print 'Is de buffer'
#        #print current_buffer
#        #print 'Er is zoveel energie verloren gegaan: '
#        #print capacity_lost
#        #print 'Er is zoveel energie niet geleverd'
#        #print not_delivered                         
    
#    #print '===== einde maand ======='        
#    #print 'De current_buffer = '
#    #print current_buffer
#    #print 'Er is zoveel energie verloren gegaan: '
#    #print capacity_lost
#    #print 'Er is zoveel energie niet geleverd'
#    #print not_delivered    
